import { RootObject } from "./types/rootObject";
import request from "./request/requestToken";
import requestExport from "./request/requestExport";
import { exportFile } from './export';
import { 
  AddMerchantRequest, 
  UpdateMerchantRequest, 
  AuditMerchantRequest, 
  DeleteMerchantRequest,
  MerchantDetail,
  MerchantPageRequest,
  MerchantPageResponse,
  MerchantByNameItem,
  MrsTenant,
  MerchantQueryResponse
} from './types/merchant';
import {
  AddEnterpriseRequest,
  UpdateEnterpriseRequest,
  EnterpriseDetail,
  EnterprisePageRequest,
  EnterprisePageResponse,
  EnterpriseTreeNode
} from './types/enterprise';
import {
  MrsRentAddRequest,
  MrsRent,
  MrsGroupAddRequest,
  MrsGroup,
  MrsRentQueryResult,
  MrsGroupQueryByCodeRequest,
  GroupQueryParams,
  GroupListResponse
} from './types/rent';
import {
  MrsLocationAddRequest,
  MrsLocation,
  LocationQueryParams,
  LocationListResponse,
  LocationTreeNode
} from './types/location';
import {
  MrsEquipUsageAddRequest,
  MrsEquipUsage,
  MrsEquipUsageQueryResult,
  MrsEquipUsageQueryRequest
} from './types/equipUsage';
import {
  ObjectFeeDetailPageRequest,
  ObjectFeeDetailPageResponse
} from './types/objectFeeDetail';

// 抄表结算

// 查询项目目录
export const getProjectList = () =>
  request<RootObject<any>>({
    method: "POST",
    url: "/acq-project/getList",
  });

// 获取房间号列表
export const getHouseList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-house/getHouseNumList",
    data,
  });

// 查询能耗总览
export const getDashboard = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/energy/getUsage",
    data,
  });

// 查询用能趋势
export const getTrend = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/energy/getUsageTrend",
    data,
  });

// 房间监控列表
export const getMonitorList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-house/getMonitor",
    data,
  });

// 监控详情-获取变量信息
export const getVarMsg = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-house/getVarMsg",
    data,
  });

// 监控详情-获取开合闸记录
export const getSwitch = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/acq-equip/switch/record",
    data,
  });

  // 监控详情-开合闸
export const goSwitch = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/acq-equip/switch",
    data,
  });

// 监控详情-获取历史变量信息
export const getVarMsgHistory = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-house/getVarMsg/history",
    data,
  });

// 监控详情
export const getMonitorDetail = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-house/getMonitorDetail",
    data,
  });

// 用户余额列表
export const getBalanceList = (params: any, data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-user/balancePage",
    params,
    data,
  });

// 用户列表
export const getUserList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-user/page",
    data,
  });

// 账单明细列表
export const getBillDetailList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-bill-detail/page",
    data,
  });

// 账单详情
export const getBillDetail = (id: string) =>
  request<RootObject<any>>({
    method: "GET",
    url: `/mrs-bill-detail/detail/${id}`,
  });

// 账单明细导出
export const exportData = (data: any) =>
  requestExport<RootObject<any>>({
    method: "POST",
    url: "/mrs-bill-detail/export",
    responseType: "blob",
    data,
  });

// 交易明细列表
export const getRecordList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-trade-detail/page",
    data,
  });

// 组织列表
export const getOrg = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/sys-org/list",
    data,
  });

// 根据组织机构和场景名称查询场景
export const getScene = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/acq-project/sceneByOrgId",
    data,
  });

// 绑定项目
export const addScene = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/acq-project/add",
    data,
  });

// 房间列表
export const getHouses = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-house/page",
    data,
  });

// 房间新增
export const addHouse = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-house/add",
    data,
  });

// 房间更新
export const editHouse = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-house/update",
    data,
  });

// 房间可选设备列表
// export const getEquipmentList = (data: any) =>
//   request<RootObject<any>>({
//     method: "POST",
//     url: `/acq-equip/projectList`,
//     data
//   });

  // 房间可选设备列表（新）
export const getEquipmentList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: `/acq-equip/project/canUseList`,
    data
  });


// 房间详情
export const getHouseDetail = (id: any) =>
  request<RootObject<any>>({
    method: "GET",
    url: `/mrs-house/detail/${id}`,
  });

// 房间删除
export const dellHouse = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-house/delete",
    data,
  });

// 用户新增
export const addUser = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-user/add",
    data,
  });

// 用户更新
export const editUser = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-user/update",
    data,
  });

// 用户详情
export const getUserDetail = (id: any) =>
  request<RootObject<any>>({
    method: "GET",
    url: `/mrs-user/detail/${id}`,
  });

// 用户删除
export const dellUser = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-user/delete",
    data,
  });

// 收费设置列表
export const getChargeList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-charge-set/page",
    data,
  });

// 收费设置新增
export const addCharge = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-charge-set/add",
    data,
  });

// 收费设置更新
export const editCharge = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-charge-set/update",
    data,
  });

// 收费设置详情
export const getChargeDetail = (id: any) =>
  request<RootObject<any>>({
    method: "GET",
    url: `/mrs-charge-set/detail/${id}`,
  });

// 收费设置删除
export const dellCharge = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-charge-set/delete",
    data,
  });

// ==================== 商户管理接口 ====================

// 商户分页查询
export const getMerchant = (data: MerchantPageRequest) =>
  request<RootObject<MerchantPageResponse>>({
    method: "POST",
    url: "/mrs-tenant/page",
    data,
  });

// 新增商户
export const addMerchant = (data: AddMerchantRequest) =>
  request<RootObject<null>>({
    method: "POST",
    url: "/mrs-tenant/add",
    data,
  });

// 更新商户
export const editMerchant = (data: UpdateMerchantRequest) =>
  request<RootObject<null>>({
    method: "POST",
    url: "/mrs-tenant/update",
    data,
  });

// 审核商户
export const examineMerchant = (data: AuditMerchantRequest) =>
  request<RootObject<null>>({
    method: "POST",
    url: "/mrs-tenant/audit",
    data,
  });

// 商户详情
export const getMerchantDetail = (id: number) =>
  request<RootObject<MerchantDetail>>({
    method: "GET",
    url: `/mrs-tenant/detail/${id}`,
  });

// 删除商户（批量）
export const dellMerchant = (data: DeleteMerchantRequest) =>
  request<RootObject<null>>({
    method: "POST",
    url: "/mrs-tenant/delete",
    data,
  });

// 根据名称查询商户列表 - 使用新接口
export const getMerchantByName = (name?: string) =>
  request<RootObject<MrsTenant[]>>({
    method: "GET",
    url: `/mrs-tenant/queryByName`,
    params: {
      name: name || ''
    }
  });

// ==================== 企业管理接口 ====================

// 企业分页查询
export const getEnterprise = (data: EnterprisePageRequest) =>
  request<RootObject<EnterprisePageResponse>>({
    method: "POST",
    url: "/mrs/enterprise/page",
    data,
  });

// 新增企业
export const addEnterprise = (data: AddEnterpriseRequest) =>
  request<RootObject<null>>({
    method: "POST",
    url: "/mrs/enterprise/add",
    data,
  });

// 更新企业
export const editEnterprise = (data: UpdateEnterpriseRequest) =>
  request<RootObject<null>>({
    method: "POST",
    url: "/mrs/enterprise/update",
    data,
  });

// 企业详情
export const getEnterpriseDetail = (id: number) =>
  request<RootObject<EnterpriseDetail>>({
    method: "GET",
    url: `/mrs/enterprise/detail/${id}`,
  });

// 删除企业（单个）
export const deleteEnterpriseById = (id: number) =>
  request<RootObject<null>>({
    method: "POST",
    url: `/mrs/enterprise/delete/${id}`,
  });
  
// 批量删除企业
export const batchDeleteEnterprise = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs/enterprise/batchDelete",
    data,
  });

// 对企业进行结算（单个）
export const settleEnterpriseById = (id: number) =>
  request<RootObject<null>>({
    method: "POST",
    url: `/mrs/enterprise/settle/${id}`,
  });

// 结算多个企业费用
export const settleBatchEnterprise = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs/enterprise/settleBatch",
    data,
  });

// 获得企业树形结构
export const getEnterpriseTree = () =>
request<RootObject<EnterpriseTreeNode[]>>({
  method: "GET",
  url: "/mrs/enterprise/tree"
});

// 获得企业列表
export const getEnterpriseList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs/enterprise/getEnterpriseList",
    data,
  });

// 设备查询列表
export const getEquipList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-house/equipPage",
    data,
  });

// 监控查询列表
export const getMonitorEctList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-house/equipPage/ect",
    data,
  });

// 收费模板列表
export const getTemplateList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-charge-template/page",
    data,
  });

// 收费设置模板列表
export const getChargeTemplateList = () =>
  request<RootObject<any>>({
    method: "GET",
    url: "/mrs-charge-template/getChargeSetTempleList",
  });

// 收费模板新增
export const addTemplate = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-charge-template/add",
    data,
  });

// 收费模板更新
export const editTemplate = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-charge-template/update",
    data,
  });

// 收费模板详情
export const getTemplateDetail = (id: any) =>
  request<RootObject<any>>({
    method: "GET",
    url: `/mrs-charge-template/detail/${id}`,
  });

// 收费模板删除
export const dellTemplate = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs-charge-template/delete",
    data,
  });

// 获取房间号
/*houseNum	房间号	string	
id	主键id	integer(int64)*/
export type homeNumList = {
  houseNum: string;
  id: number;
}[];
export const getHouseIdList = (id: number) =>
  request<RootObject<homeNumList>>({
    method: "POST",
    url: "/mrs-house/getHouseNumList",
    data: { id },
  });

  // 获取数据报表列表
export const getReportList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/energy/report/data/list",
    data,
  });

// 导出
export const exportReport = (data: any) =>
  exportFile('/energy/report/data/export', '', data);

// 补抄记录
export const getBatchList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/energy/report/data/patch/list",
    data,
  });

// 数据补抄
export const dataBatch = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/energy/report/data/patch",
    data,
  });

// 再次补抄
export const batchDouble = (id: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: `/energy/report/data/patch/again/${id}`,
  });

// 文件上传
export const uploadFile = (file: FormData) =>
  request<RootObject<{ url: string }>>({
    method: "POST",
    url: "/file/upload",
    data: file,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });

// ==================== 房租管理接口 ====================

// 新增房租
export const addRent = (data: MrsRentAddRequest) =>
  request<RootObject<object>>({
    method: "POST",
    url: "/mrs/rent/add",
    data,
  });

// 房租详情
export const getRentDetail = (id: number) =>
  request<RootObject<MrsRent>>({
    method: "GET",
    url: `/mrs/rent/detail/${id}`,
  });

// 更新房租
export const editRent = (data: MrsRent) =>
  request<RootObject<null>>({
    method: "POST",
    url: "/mrs/rent/update",
    data,
  });

// 删除房租
export const deleteRent = (id: number) =>
  request<RootObject<null>>({
    method: "POST",
    url: `/mrs/rent/delete/${id}`,
  });

// ==================== 分组管理接口 ====================

// 新增分组
export const addGroup = (data: MrsGroupAddRequest) =>
  request<RootObject<object>>({
    method: "POST",
    url: "/mrs/group/add",
    data,
  });

// 根据分组编码获取分组信息列表
export const getGroupListByCode = (data: MrsGroupQueryByCodeRequest) =>
  request<RootObject<MrsGroup[]>>({
    method: "POST",
    url: "/mrs/group/getMrsGroupListByCode",
    data,
  });

// 获取企业的所有分组信息
export const getGroupListByEnterprise = (data: MrsGroupQueryByCodeRequest) =>
  request<RootObject<MrsGroup[]>>({
    method: "POST",
    url: "/mrs/group/getMrsGroupList",
    data,
  });

// 获取房租信息列表（根据分组ID）
export const getRentListByGroupId = (groupId: number) =>
  request<RootObject<MrsRentQueryResult[]>>({
    method: "GET",
    url: `/mrs/rent/list/${groupId}`,
  });

// 分组分页查询
export const getGroupList = (data: GroupQueryParams) =>
  request<RootObject<GroupListResponse>>({
    method: "POST",
    url: "/mrs/group/page",
    data,
  });

// 更新分组
export const editGroup = (data: MrsGroup) =>
  request<RootObject<null>>({
    method: "POST",
    url: "/mrs/group/update",
    data,
  });

// 删除分组
export const deleteGroup = (id: number) =>
  request<RootObject<null>>({
    method: "POST",
    url: `/mrs/group/delete/${id}`,
  });

// 更新分组
export const existByGroupCode = (data: MrsGroupQueryByCodeRequest) =>
  request<RootObject<null>>({
    method: "POST",
    url: "/mrs/group/existByGroupCode",
    data,
  });

// ==================== 地点位置管理接口 ====================

// 新增地点位置
export const addLocation = (data: MrsLocationAddRequest) =>
  request<RootObject<object>>({
    method: "POST",
    url: "/mrs/location/add",
    data,
  });

// 地点位置分页查询
export const getLocationList = (data: LocationQueryParams) =>
  request<RootObject<LocationListResponse>>({
    method: "POST",
    url: "/mrs/location/page",
    data,
  });

// 地点位置详情
export const getLocationDetail = (id: number) =>
  request<RootObject<MrsLocation>>({
    method: "GET",
    url: `/mrs/location/detail/${id}`,
  });

// 更新地点位置
export const editLocation = (data: MrsLocation) =>
  request<RootObject<null>>({
    method: "POST",
    url: "/mrs/location/update",
    data,
  });

// 删除地点位置
export const deleteLocation = (id: number) =>
  request<RootObject<null>>({
    method: "POST",
    url: `/mrs/location/delete/${id}`,
  });

// 获取地点位置树形结构
export const getLocationTree = (tenantId: number) =>
  request<RootObject<LocationTreeNode[]>>({
    method: "GET",
    url: `/mrs/location/tree/${tenantId}`,
  });


// ==================== 设备用量管理接口 ====================
// 新增设备用量
export const addEquipUsage = (data: MrsEquipUsageAddRequest) =>
  request<RootObject<object>>({
    method: "POST",
    url: "/mrs/equip-usage/add",
    data,
  });

// 设备用量详情
export const getEquipUsageDetail = (id: number) =>
  request<RootObject<MrsEquipUsage>>({
    method: "GET",
    url: `/mrs/equip-usage/detail/${id}`,
  });

// 更新设备用量
export const editEquipUsage = (data: MrsEquipUsage) =>
  request<RootObject<null>>({
    method: "POST",
    url: "/mrs/equip-usage/update",
    data,
  });

// 删除设备用量
export const deleteEquipUsage = (id: number) =>
  request<RootObject<null>>({
    method: "POST",
    url: `/mrs/equip-usage/delete/${id}`,
  });

// 获取设备用量信息列表（根据分组ID）
export const getEquipUsageListByGroupId = (data: MrsEquipUsageQueryRequest) =>
  request<RootObject<MrsEquipUsageQueryResult[]>>({
    method: "POST",
    url: `/mrs/equip-usage/list`,
    data,
  });
// 结算单汇总分页查询
export const getSettleBillTotalList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs/settle-bill-total/page",
    data,
  });

// 获取结算单汇总详情
export const getSettleBillTotalDetail = (id: number) =>
  request<RootObject<any>>({
    method: "GET",
    url: `/mrs/settle-bill-total/detail/${id}`,
  });

// 更新结算单为已支付状态
export const updateSettleBillTotalPaid = (id: number) =>
  request<RootObject<boolean>>({
    method: "GET",
    url: `/mrs/settle-bill-total/paid/${id}`,
  });
  
// 确认结算单汇总
export const confirmSettleBillTotal = (data: any) =>
request<RootObject<boolean>>({
  method: "POST",
  url: `/mrs/settle-bill-total/confirm`,
  data,
});
  
// 更新结算单汇总
export const updateSettleBillTotal = (data: any) =>
request<RootObject<boolean>>({
  method: "POST",
  url: `/mrs/settle-bill-total/update`,
  data,
});

// 重新结算指定结算单汇总ID的结算单数据
export const resettleBillTotal = (data: any) =>
request<RootObject<boolean>>({
  method: "POST",
  url: `/mrs/settle-bill-total/resettle`,
  data,
});

// 导出全部结算单汇总
export const exportSettleBillTotal = (data: any) => 
  exportFile('/mrs/settle-bill-total/exportAll', '', data)

// 发送结算单邮件
export const sendSettleBillEmail = (data: any) =>
request<RootObject<boolean>>({
  method: "POST",
  url: `/mrs/settle-bill-total/sendSettleBillEmail`,
  data,
});

// 获取结算操作权限
export const getSettlePermissions = () =>
request<RootObject<boolean>>({
  method: "POST",
  url: `/mrs/settle-bill-total/getSettlePermissions`,
});

// 获取结算单明细详情
export const getSettleBillDetailDetail = (id: number) =>
  request<RootObject<any>>({
    method: "GET",
    url: `/mrs/settle-bill-detail/detail/${id}`,
  });

// 结算单明细分页查询
export const getSettleBillDetailList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs/settle-bill-detail/page",
    data,
  });
    
// 更新结算单明细
export const updateSettleBillDetail = (data: any) =>
request<RootObject<boolean>>({
  method: "POST",
  url: `/mrs/settle-bill-detail/update`,
  data,
});
  
// 确认结算单明细
export const confirmSettleBillDetail = (data: any) =>
request<RootObject<boolean>>({
  method: "POST",
  url: `/mrs/settle-bill-detail/confirm`,
  data,
});
    
// 新增人工创建的结算单明细
export const addManualSettleBillDetail = (data: any) =>
request<RootObject<boolean>>({
  method: "POST",
  url: `/mrs/settle-bill-detail/addManual`,
  data,
});
    
// 更新人工创建的结算单明细
export const updateManualSettleBillDetail = (data: any) =>
request<RootObject<boolean>>({
  method: "POST",
  url: `/mrs/settle-bill-detail/updateManual`,
  data,
});

// 对象费用明细分页查询
export const getObjectFeeDetailList = (data: any) =>
  request<RootObject<any>>({
    method: "POST",
    url: "/mrs/object-fee-detail/page",
    data,
  });
    
// 更新对象费用明细
export const updateObjectFeeDetail = (data: any) =>
request<RootObject<boolean>>({
  method: "POST",
  url: `/mrs/object-fee-detail/update`,
  data,
});
  
// 确认对象费用明细
export const confirmObjectFeeDetail = (data: any) =>
request<RootObject<boolean>>({
  method: "POST",
  url: `/mrs/object-fee-detail/confirm`,
  data,
});

// ==================== 设备采集数据接口 ====================
// 设备采集数据分页查询
export const getEquipCollectDataList = (data: any) =>
request<RootObject<any>>({
  method: "POST",
  url: "/mrs/equip-collect-data/page",
  data,
});

// 新增设备采集数据
export const addEquipCollectData = (data: any) =>
  request<RootObject<object>>({
    method: "POST",
    url: "/mrs/equip-collect-data/add",
    data,
  });

// 更新设备采集数据
export const editEquipCollectData = (data: any) =>
  request<RootObject<null>>({
    method: "POST",
    url: "/mrs/equip-collect-data/update",
    data,
  });

// 删除设备采集数据
export const deleteEquipCollectData = (id: number) =>
  request<RootObject<null>>({
    method: "POST",
    url: `/mrs/equip-collect-data/delete/${id}`,
  });
  
// 导入设备采集数据
export const importEquipCollectData = (file: FormData) =>
  request<RootObject<{ url: string }>>({
    method: "POST",
    url: "/mrs/equip-collect-data/importEquipCollectData",
    data: file,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
  
// 查询设备信息列表
export const queryByEquipNum = (data: any) =>
  request<RootObject<null>>({
    method: "POST",
    url: "/acq-equip/queryByEquipNum",
    data,
  });
  
// 下载导入数据模板
export const downloadDataImportTemplate = (data: any) => 
exportFile('/mrs/equip-collect-data/downloadDataImportTemplate', '', data)